package com.base.test.mutual.utils;

import com.base.commons.tools.utils.CameTools;
import com.base.commons.tools.utils.HttpConnectUtil;
import com.base.test.mutual.properties.LmsProperties;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import java.util.HashMap;

@Log4j2
@Configuration
@EnableConfigurationProperties(LmsProperties.class)
public class LmsHttpUtils {

    @Autowired
    private LmsProperties lmsProperties;

    public  <T> T execute(T requestObj, String url,String method, Class<?> clazz){
        String request = null;
        ResponseEntity res=null;
        try {
            url=lmsProperties.getUrl()+url;
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.AUTHORIZATION,lmsProperties.getSecret());
            request = CameTools.toUnderlineJSONString(requestObj);
            log.info("模块Module:{},url:{},请求参数[{}],请求结果：{}","LMS",url,request);
            res = HttpConnectUtil.tempHttp(request, url, headers, method, new HashMap<>(), clazz);
            log.info("模块Module:{},url:{},请求结果：{}","LMS",url,res.getBody());
        }catch (Exception e) {
            log.error("模块Module:{},url:{},请求参数[{}],异常信息：{}","LMS",url,request,e);
        }
        return (T) res.getBody();
    }
}
